package com.zelin.controller;

import com.zelin.pojo.Books;
import com.zelin.pojo.Result;
import com.zelin.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @PostMapping("list")
    public List<Books> list(int cid){
        return bookService.findAll(cid);
    }
    @PostMapping("add")
    public Result add(@RequestBody Books books){
        System.out.println(books);
        Result result = null;
        try {
            bookService.add(books);
            result=new Result(true,"success");
        } catch (Exception e) {
            e.printStackTrace();
            result=new Result(false,"失败");
        }
        return result;
    }
    @PostMapping("update")
    public Result update(@RequestBody Books books){
        System.out.println(books);
        Result result = null;
        try {
            bookService.up(books);
            result=new Result(true,"success");
        } catch (Exception e) {
            e.printStackTrace();
            result=new Result(false,"失败");
        }
        return result;
    }
    @PostMapping("delete")
    public void delete(int bid){
        bookService.del(bid);
    }
}
